// 调用验证码接口
function loadVeri () {
  $.get("http://127.0.0.1:3000/users/veri", { action: "getVeri" }, function (res) {
    draw(res.veri)  //res.veri 从后端获取到的验证码 abcd 
  })
}
loadVeri()

function draw (code) { //绘制函数

  function getColor () { //随机颜色函数
    var color = '#' + Math.floor(Math.random() * 0xffffff).toString(16);
    return color
  }
  function ranDom (min, max) { //随机函数
    return Math.floor(Math.random() * (max - min) + min);
  }

  var ctx = document.getElementById("mycanvas");
  console.log(ctx)
  var c = ctx.getContext("2d");
  for (var i = 0; i < 4; i++) {
    c.fillStyle = getColor();
    c.font = "25px 黑体"
    c.fillText(code[i], i * 35 + ranDom(0, 20), ranDom(15, 30))
  }
  function loadPonit () {
    // 绘制干扰点
    for (var j = 0; j < 50; j++) {
      c.beginPath();
      var x = ranDom(0, 160)
      var y = ranDom(0, 35)
      c.strokeStyle = getColor();
      c.moveTo(x, y)
      c.lineTo(x + 1, y + 1)
      c.stroke();
    }
  }
  loadPonit();
  // 绘制干扰线
  function loadLine () {
    for (var i = 0; i < 2; i++) {
      c.beginPath();
      c.strokeStyle = getColor();
      c.moveTo(ranDom(0, 160), ranDom(0, 35))
      c.lineTo(ranDom(0, 160), ranDom(0, 35))
      c.stroke();
    }
  }
  loadLine()
}
